#include "stdafx.h"

#pragma once

struct QueueNode
{
	const void * data;
	QueueNode * prev;
	QueueNode * next;
};

void Enqueue(QueueNode ** front, QueueNode ** rear, const void * data)
{
	QueueNode * newNode = (QueueNode *)malloc(sizeof(QueueNode));

	if(!newNode)
	{
		return;
	}

	newNode->data = data;
	newNode->next = NULL;

	if(*front == NULL)
	{
		newNode->prev = NULL;

		*front = newNode;
		*rear = *front;
	}
	else
	{
		newNode->prev = *rear;

		(*rear)->next = newNode;
		*rear = (*rear)->next;
	}
}

const void * Dequeue(QueueNode ** front, QueueNode ** rear)
{
	if(*front == NULL)
	{
		return NULL;
	}

	QueueNode * qnode = *front;
	const void * data = qnode->data;

	if(*front == *rear)
	{
		*front = NULL;
		*rear = NULL;
	}
	else
	{
		*front = (*front)->next;
		(*front)->prev = NULL;
	}

	free(qnode);

	return data;
}